#include "geminai_get_temp.h"
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/timers.h>
#include <string.h>
#include "geminai_para.h"
#include "geminai_adc.h"
TimerHandle_t xTimer_temp;
#define TIMER_PERIOD pdMS_TO_TICKS(1000) // 定时器间隔为1秒

/**
 * @brief 定时获取温度
 * 
 */
void get_temp_1hz(TimerHandle_t xTimer)
{		
	static uint8_t j=0;
	static float temp1=0;
	if( !sys_temp.org_temp_init )//获取刚上电的初始温度
	{
		sys_temp.real_temp= ntc0_get_temp();
		sys_temp.org_temp_init = 1;//标记已获取到刚上电的温度
		sys_temp.orgin_temp = sys_temp.real_temp;
		sys_temp.temp_per_min=sys_temp.real_temp;//一开始平均1分钟温度没有 默认刚获取到的温度为平均1分钟温度
		return;
	}
	sys_temp.around_temp = get_NTCrx();
	// printf("NA = %f\n", sys_temp.around_temp);
	// printf("sys_temp.real_temp = %f\n", sys_temp.real_temp);
	temp1 += ntc0_get_temp();
	j++;
	if(j>=3)
	{			
		sys_temp.real_temp = temp1 / 3;
		j=0;
		temp1=0;
	}
        
	if(sys_temp.sec_cnt < 60)
	{
		sys_temp.sec_cnt++;
		sys_temp.temp_per_min_t += sys_temp.real_temp;
	}
	else
	{
		sys_temp.temp_per_min = sys_temp.temp_per_min_t/60;

		sys_temp.sec_cnt = 1;//新的计算周期
		sys_temp.temp_per_min_t = sys_temp.real_temp;

		if(sys_temp.min_cnt < 60)
		{
			sys_temp.min_cnt++;
			sys_temp.temp_per_hour_t += sys_temp.temp_per_min;
		}
		else
		{
			sys_temp.temp_per_hour = sys_temp.temp_per_hour_t/60;

			sys_temp.min_cnt = 1;//新的计算周期
			sys_temp.temp_per_hour_t = sys_temp.temp_per_min;
		}
	}
	if(sys_temp.real_temp > TEMP_ERR_UPPER || sys_temp.real_temp < TEMP_ERR_LOWER)
		alarm_stat |= ALARM_NTC1_ERROR;
	else
		alarm_stat &=~ ALARM_NTC1_ERROR;


	static uint8_t time_overtemp_cnt = 0;

	//高温警报延时时间
	if(time_overtemp_cnt++ >= 60)
	{
		if(overtemp_time > 0)
			overtemp_time--;
	
		time_overtemp_cnt = 0;
		//达到超温（超过温度阈值）报警条件
		if((sys_temp.temp_per_min > temp_ctl_para.lowtemp && sys_temp.temp_per_min < temp_ctl_para.overtemp) || sys_temp.temp_per_min < TEMP_ERR_LOWER || sys_temp.temp_per_min > TEMP_ERR_UPPER)
		{			
			if(overtemp_time < temp_ctl_para.overtemp_alarm_delay_time)
				overtemp_time = temp_ctl_para.overtemp_alarm_delay_time;	
		}
	}

	if(overtemp_time == 0 )
	{
		if(temp_ctl_para.overtemp_flag == 0 && sys_temp.temp_per_min > temp_ctl_para.overtemp)
		{
			temp_ctl_para.overtemp_flag = 1;			
			alarm_stat |= ALARM_OVERTEMP;	
			frig_event_set(EVENT_OVERTEMP_ALARM);//发送高温报警
		}
		if(temp_ctl_para.lowtemp_flag == 0 && sys_temp.temp_per_min < temp_ctl_para.lowtemp)
		{
			temp_ctl_para.lowtemp_flag = 1;			
			alarm_stat |= ALARM_LOWTEMP;
			frig_event_set(EVENT_LOWTEMP_ALARM);//发送低温报警
		}
	}
	else
	{
		if(temp_ctl_para.overtemp_flag == 1)
		{
			temp_ctl_para.overtemp_flag = 0;
			alarm_stat &=~  ALARM_OVERTEMP;
			frig_event_set(EVENT_OVERTEMP_ALARM);//发送解除高温报警
		}
		if(temp_ctl_para.lowtemp_flag == 1)
		{
			temp_ctl_para.lowtemp_flag = 0;
			alarm_stat &=~  ALARM_LOWTEMP;
			frig_event_set(EVENT_LOWTEMP_ALARM);//发送解除低温报警
		}
	}
	/*应对双85测试，当温度高于65℃时关闭4g模块*/
	if(sys_temp.around_temp > 65)
	{
		POW4G_EN_OFF;
	}

}

void get_temp_task(void)
{
	xTimer_temp = xTimerCreate("Timer", TIMER_PERIOD, pdTRUE, (void *)0, get_temp_1hz);
	xTimerStart(xTimer_temp, 0);
}
